﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using System.Data.SqlClient;
using System.Data;
using Register.Model;
using Register.DBUtility;

namespace Register.DAL
{
    /// <summary>
    /// FileName: ParamterDAL.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangYan
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/6/23 21:27:19
    /// </summary>
    public class ParamterDAL:IParameter
    {

        /// <summary>
        /// 根据类型获取参数配置列表
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public List<Parameter> GetParametersByType(string type)
        {
            String sql = " select * from System_parameter where SystemParameter_Type=@type and SystemParameter_Status='1' ";
            SqlParameter[] paramters = new SqlParameter[] { 
            new SqlParameter("@type",SqlDbType.VarChar)
            };
            paramters[0].Value = type;
            return ChangeReaderToList(DbHelperSQL.ExecuteReader(sql,paramters));
        }

        private List<Parameter> ChangeReaderToList(SqlDataReader reader)
        {
            List<Parameter> list = new List<Parameter>();
            while (reader.Read())
            {
                Parameter paramter= new Parameter();
                paramter.SystemParameter_ID = reader.GetString(1);
                paramter.SystemParameter_Name = reader.GetString(2);
                list.Add(paramter);
            }
            return list;
        }

        /// <summary>
        /// 根据类型和编号获取参数值
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="ID">编号</param>
        /// <returns></returns>
        public string GetParameterNameByTypeAndID(string type, string ID)
        {
            String sql = "select SystemParameter_Name from System_parameter where SystemParameter_ID=@ID and SystemParameter_Type=@type ";
            SqlParameter[] parameters = new SqlParameter[] { 
            new SqlParameter("@ID",SqlDbType.VarChar),
            new SqlParameter("@type",SqlDbType.VarChar)
            };
            parameters[0].Value = ID;
            parameters[1].Value = type;

            return (String)DbHelperSQL.Query(sql, parameters).Tables[0].Rows[0]["SystemParameter_Name"];
        }
    }
}
